<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>注册页面</h1>
<form action="">
    <input type="text" name="username" @blur="blurfn()" placeholder="用户名">
    <span>{{info}}</span>
    <input type="text" name="password" placeholder="密码">
    <input type="text" name="nick" placeholder="昵称">
    <input type="text" name="age" placeholder="年龄">
    <input type="button" value="注册" @click="reg()">
</form>
<!--引入vue-->
<script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
<!--引入axios框架-->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script>
    let vm = new Vue({
        el:"form",
        data:{
            info:""
        },
        methods:{
            blurfn:function () {
                let data = new FormData(document.querySelector("form"));
                let username = data.get("username");
                if(username==""){
                    vm.info="用户名不能为空!"
                    return;
                }
                axios.post("/check",data).then(function (response) {
                    //把服务器返回的数据给到info变量,页面自动变
                    vm.info = response.data;
                })
            },
            reg:function () {
                let data = new FormData(document.querySelector("form"));
                axios.post("/reg",data).then(function (response) {

                    if(response.data==1){//1一般代表成功!
                        alert("注册成功!");
                        //浏览器向首页发出请求
                        location.href = "/";
                    }else{//2代表用户名已存在
                        alert("用户名已存在!")
                    }
                })
            }
        }
    })
</script>
</body>
</html>